/**
 * 订单录入控制器
 */
import './index.less'
import $_fn from '~plugins'
import '~plugins/ueditor/ueditor.config.js'
import '~plugins/ueditor/lang/zh-cn/zh-cn.js'
import '~plugins/ueditor/ueditor.all.min.js'
//import '~plugins/lib/js/jquery.base64.js'

export default ['$scope','$rootScope','$location','$ui','http','uibDateParser','$timeout','$state',function($scope,$rootScope,$location,$ui,http,uibDateParser,$timeout,$state) {
    // 传过来的 贷款方式    //借款类型(1.车贷 2房贷 3信贷)
    $scope.searchData = $location.search()
    $scope.loanState = $location.search().state 
    console.log('localtion', $location.search(),$location)
    //各表单在页面的定位 常量
    $scope.LOAN_MESS_NUM = '200'
    $scope.BASE_MESS_NUM = '500'
    // $scope.cityid1= "19,291,3058"
    // $scope.cityid2= "19,291,3058"
    $scope.apply = {'loanMess':true}
    $scope.state = 1    //选择的贷款方式
    $scope.err = {}  //错误边框是否提示
    $scope.errTip = {}  //错误边框是否提示
    $scope.checked = {}
    //借款人错误信息提示初始化
    let _errTis = {real_name: false, id_card: false, birthdate: false, phone: false, sex: false}
    $scope.errTip.borrTis = [_errTis]  //错误边框是否提示-借款人信息
    $scope.option = {}  //下拉框每个下拉显示数据
    $scope.uploads = {} //上传信息存储
    $scope.msgFill = {}//资料清单是否填写
    $scope.msgFill.applyOk = true  //test
    $scope.attaArr = []     //数据列表数组
    $scope.numId = 0
    // $scope.errTip.loanType = true
    //初始化-方便查看
    $scope.data = {
        /* 借款信息 */
        fee: {},
        order: {},
        /* 基本信息 */
        person: [{id:null, real_name: '', id_card: '', birthdate: '', phone: '', sex: ''}],
        /* 公司信息 */
        company: {},
        /* 车辆信息 */
        car: {},
        /* 房产信息 */
        housing: {},
        propertyPersonal: [{real_name: null, ratio: null}],
        /* 联系人信息 */
        linkMan: {},
        /* 其他联系人信息 */
        json1: {},
        json2: {},
        json3: {},
        /* 附件资料 */
        orderAttachmentList: {}
    }
    $scope.data.personOne = {}
    
    // $scope.data.order.loan_type = '2'
    // $scope.data.personOne.begin_time = '1990-12-11' 
    //警告提示
    $scope.errorToast = function(text) {
        $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    //成功提示
    $scope.successToast = function(text) {
        $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
    }
    
    //获取多种下拉选项类型列表
    http.post('selectOrderInit.do', {}, function (data) {			
        console.log('ryy-selectOrderInit', data)
        $scope.$apply(function(){
            //贷款方式（订单类型）
            data.data.loanTypeList.unshift({id:'', lt_name:'选择类型'})
            $scope.data.order.lt_id = ''
            $scope.option.loanType = data.data.loanTypeList    
            //订单来源
            data.data.orderSourceList.unshift({id:'', os_name:'选择来源'})
            $scope.data.order.os_id = ''
            $scope.option.orderSource = data.data.orderSourceList
            //资料清单
            data.data.attatchmentTypeList.unshift({id:'', type_name:'选择类型'})
            $scope.uploads.attach_id = ''
            $scope.option.dataList = data.data.attatchmentTypeList    
            //申请期限
            data.data.applyTermList.unshift({id:'', at_name:'选择期限'})
            $scope.data.order.at_id = ''
            $scope.option.period = data.data.applyTermList 
            //教育程度
            data.data.educationLevelList.unshift({id:'', education:'请选择'})
            $scope.data.personOne.education = ''
            $scope.option.education = data.data.educationLevelList 
            //职位身份
            data.data.positionList.unshift({id:'', position_name:'请选择'})
            $scope.data.personOne.position = ''
            $scope.option.positionList = data.data.positionList 
            //还款方式
            $scope.data.order.repayment_way = ''
            $scope.option.replyType = [{name:'选择方式',id:''},{name:'先息后本',id:'1'},{name:'等额等息',id:'2'}]
            //选择性别
            $scope.option.sexArr = [{name:'选择性别',id:''},{name:'男',id:'1'},{name:'女',id:'2'}]
            //婚姻状况
            $scope.data.personOne.marry = ''
            $scope.option.marital = [{name:'请选择',id:''},{name:'已婚',id:'1'},{name:'未婚',id:'2'},{name:'未知',id:'3'}]
            //微粒贷状况
            $scope.option.qqloan = [{name:'请选择',id:''},{name:'有',id:'1'},{name:'无',id:'2'}]
            //gps 付款方式
            $scope.data.fee.gps = '1'
            $scope.option.gpsPay = [{name:'月付',id:'1'},{name:'一次性',id:'2'}]
        })

        if(data.errcode == 1){
            // 编辑订单
            if($scope.searchData.oid && $scope.searchData.pid){
                http.post('editOrderInfo.do', {oid:$scope.searchData.oid, pid:$scope.searchData.pid}, function (data) {
                    console.log(data)
                    $scope.$apply(function(){
                        $scope.data = data.data
                        $scope.data.personOne = data.data.person[0]
                        //户口地址 省市区
                        let personOne = data.data.person[0]
                        if(personOne.resid_province){
                            $scope.cityid1 = personOne.resid_province +','+ personOne.resid_city +','+ personOne.resid_area
                        }  
                        //现住地址 省市区
                        if(personOne.addr_province){
                            $scope.cityid2 = personOne.addr_province +','+ personOne.addr_city +','+ personOne.addr_area
                        }
                        //公司地址 省市区
                        let company = data.data.company
                        if(company.addr_province){
                            $scope.cityid3 = company.addr_province +','+ company.addr_city +','+ company.addr_area
                        }
                        //房产地址 省市区 
                        let housing = data.data.housing
                        if(housing.addr_province){
                            $scope.cityid4 = housing.addr_province +','+ housing.addr_city +','+ housing.addr_area
                        }
                        //联系人家庭地址 省市区 
                        let linkMan = data.data.linkMan
                        if(linkMan.addr_province){
                            $scope.cityid5 = linkMan.addr_province +','+ linkMan.addr_city +','+ linkMan.addr_area
                        }
                        //房产权利人
                        if(data.data.propertyPersonal && data.data.propertyPersonal.length<1){
                            $scope.data.propertyPersonal = [{real_name: null, ratio: null}]
                        }
                        //图片资料
                        if(data.data.orderAttachmentList && data.data.orderAttachmentList.length>0){
                            $scope.attaArr = data.data.orderAttachmentList
                        }

                        data.data.otherLinkMan[0] && ($scope.data.json1 = data.data.otherLinkMan[0])
                        data.data.otherLinkMan[1] && ($scope.data.json2 = data.data.otherLinkMan[1])
                        data.data.otherLinkMan[2] && ($scope.data.json3 = data.data.otherLinkMan[2])
                        console.log('edit-init', $scope.data,$scope)
                    })
                })
            }
        }
    })

    /* 身份证校验 */
    var isCarid = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    /**
     * 添加借款人
     */
    $scope.addBorrowingName = function () {
      console.log(123)
      let _name = {id:0, real_name: '', id_card: '', birthdate: '', phone: '', sex: ''} //清空
      $scope.data.person.push(_name)
      _errTis = {real_name: false, id_card: false, birthdate: false, phone: false, sex: false}
      $scope.errTip.borrTis.push(_errTis)
    }
    /**
     * 删除借款人
     */
    $scope.deleteBersonalName = function (index) {
      $scope.data.person.splice(index, 1)
    }
    /**
     * 添加房产权利人
     */
    $scope.addHouserProperty = function () {
      $scope.data.propertyPersonal.push({real_name: null, ratio: null})
    }
    /**
     * 删除房产权利人
     */
    $scope.deleteHouserProperty = function (index) {
      $scope.data.propertyPersonal.splice(index, 1)
    }
    /**
     * 跳转到指定的菜单位置
     */
    $scope.onJumpClick = function(position, nowList) {
      console.log('abc-test1')
      $_fn.stopBOM()
      $_fn.applyShow(nowList,$scope.apply)
      setTimeout(function(){
        $(".main-content").animate({scrollTop: position }, 500,"swing");
        return false;
      },1)
    }
    /**
     * 去除报错高亮
     */
    $scope.remlight = function () {
      for (var i in $scope.err) {
        $scope.err[i] = false
      }
      //新增借款人提示错误
      for (var is in $scope.errTip.borrTis) {
        for (var s in $scope.errTip.borrTis[is]) {
          $scope.errTip.borrTis[is][s] = false
        }
      }
    }
    //利息不选中时隐藏而且清空  借款信息录入
    $scope.sectItem = function(param){
        if (param == 'hand_fee') {
          if($scope.checked.hand_fee==true){
            $scope.data.fee.hand_fee = null
          }
          $scope.checked.hand_fee  = !$scope.checked.hand_fee
      }
      if (param == 'other_fee') {
        if($scope.checked.other_fee==true){
          $scope.data.fee.other_fee = null
        }
        $scope.checked.other_fee  = !$scope.checked.other_fee
      }
      if (param == 'investigate_fee') {
        if($scope.checked.investigate_fee==true){
          $scope.data.fee.investigate_fee = null
        }
        $scope.checked.investigate_fee  = !$scope.checked.investigate_fee
      }
      if (param == 'gps_fee') {
        if($scope.checked.gps_fee==true){
          $scope.data.fee.gps_fee = null
        }
        $scope.checked.gps_fee  = !$scope.checked.gps_fee
      }
    }
    /**
     * 校验身份证以及填入出生日期
     */
    $scope.getIdCard = function(key){
        // console.log(key, isCarid.test($scope.data.person[key].id_card), $_fn.Datetype($scope.data.person[key].id_card))
        if (isCarid.test($scope.data.person[key].id_card)) {
            $scope.data.person[key].birthdate = new Date($_fn.Datetype($scope.data.person[key].id_card))
        }
        // console.log($scope.data.person[key].birthdate)
    }
    $scope.saveDateHandle = function(val){
        // console.log('save-date', $scope)
    }
    /**
     * 必填校验
     */
    $scope.checkRequired = function (){
        let checkLose = false;
        //判断必填借款信息 没填变红
        if (!$scope.data.order.lt_id) {
            $scope.err.lt_id = true
            $scope.errorToast('请选择订单类型')
            return checkLose = 'loanMsg'
        }
        if (!$scope.data.order.apply_money) {
            $scope.err.apply_money = true
            $scope.errorToast('请填写申请金额')
            return checkLose = 'loanMsg'
        }
        if (!$scope.data.order.at_id) {
            $scope.err.at_id = true
            $scope.errorToast('请选择申请期限')
            return checkLose = 'loanMsg'
        }
        if (!$scope.data.order.repayment_way) {
            $scope.err.repayment_way = true
            $scope.errorToast('请选择还款方式')
            return checkLose = 'loanMsg'
        }
        
        if (!$scope.data.order.loan_way) {
            $scope.errorToast('请选择抵押方式')
            return checkLose = 'loanMsg'
        }
        if (!$scope.data.fee.interest) {
            $scope.err.interest = true
            $scope.errorToast('请填写利率')
            return checkLose = 'loanMsg'
        }
        //此处批量校验借款人
        for (let key in $scope.data.person) {
            if (!$scope.data.person[key].real_name) {
                $scope.errTip.borrTis[key].real_name = true
                $scope.errorToast('请填写借款人' + ((parseInt(key) + 1) || ''))
                return checkLose = 'base'
            }
            // console.log('data', $scope.errTip.borrTis[key].real_name)
            if (!$scope.data.person[key].phone) {
                $scope.errTip.borrTis[key].phone = true
                $scope.errorToast('请填写手机号码')
                return checkLose = 'base'
            }
            if (!/^1[35678]\d{9}$/.test($scope.data.person[key].phone)) {
                $scope.errTip.borrTis[key].phone = true
                $scope.errorToast('手机号码格式有误')
                return checkLose = 'base'
            }
            if (!$scope.data.person[key].id_card) {
                $scope.errTip.borrTis[key].id_card = true
                $scope.errorToast('请填写身份证号码')
                return checkLose = 'base'
            }
            if (!isCarid.test($scope.data.person[key].id_card)) {
                $scope.errTip.borrTis[key].id_card = true
                $scope.errorToast('身份证格式错误')
                return checkLose = 'base'
            }
            console.log('data-check',key, $scope.data.person, $scope.data.person[key].birthdate)
            if (!$scope.data.person[key].birthdate) {
                $scope.errTip.borrTis[key].birthdate = true
                $scope.errorToast('请填写出生日期')
                return checkLose = 'base'
            }
            if (!$scope.data.person[key].sex) {
                $scope.errTip.borrTis[key].sex = true
                $scope.errorToast('请选择性别')
                return checkLose = 'base'
            }
            
        }
        //收款账号校验
        if (!$scope.data.personOne.payee) {
            $scope.err.payee = true
            $scope.errorToast('请填写卡号真实姓名')
            return checkLose = 'base'
        }
        if (!$scope.data.personOne.account) {
            $scope.err.account = true
            $scope.errorToast('请填写银行卡号')
            return checkLose = 'base'
        }
        if (!$scope.data.personOne.bank) {
            $scope.err.bank = true
            $scope.errorToast('请填写银行名称')
            return checkLose = 'base'
        }
        console.log('ryy-key-last',$scope.data.person,$scope.data.personOne)
        return checkLose
    }
    /**
     * 表单提交
     */
    $scope.sel =function(sId){
        //验证 返回状态
        let requireLose = $scope.checkRequired()
        console.log('ryy-sel-data:',requireLose)
        if(requireLose){
            if(requireLose == 'loanMsg'){
                //页面定位到借款信息
                $scope.onJumpClick($scope.LOAN_MESS_NUM,'loanMess')
            }
            else if(requireLose == 'base'){
                //页面定位到基本信息
                $scope.onJumpClick($scope.BASE_MESS_NUM,'loanMess')
            }
            return
        }
        //整理准备发送的数据
        let formData = {}
        //uibDateParser angular-ui时间格式转换
        // $scope.data.personOne.begin_time = uibDateParser.filter($scope.data.personOne.begin_time,'yyyy-MM-dd')
        // $scope.data.personOne.local_time = uibDateParser.filter($scope.data.personOne.local_time,'yyyy-MM-dd')
        // $scope.data.car.car_time = uibDateParser.filter($scope.data.car.car_time,'yyyy-MM-dd')
        // $scope.data.housing.buy_time = uibDateParser.filter($scope.data.housing.buy_time,'yyyy-MM-dd')
        // for(let i=0; i<$scope.data.person.length; i++){
        //     $scope.data.person[i].birthdate = uibDateParser.filter($scope.data.person[i].birthdate,'yyyy-MM-dd')
        // }

        $scope.data.personOne.real_name = $scope.data.person[0].real_name
        $scope.data.personOne.phone = $scope.data.person[0].phone
        $scope.data.personOne.id_card = $scope.data.person[0].id_card
        $scope.data.personOne.birthdate = $scope.data.person[0].birthdate
        $scope.data.personOne.sex = $scope.data.person[0].sex
        //户口地址 省市区 
        let hukou = ($scope.cityid1||'').split(',')
        hukou[0] && ($scope.data.personOne.resid_province = hukou[0])
        hukou[1] && ($scope.data.personOne.resid_city = hukou[1])
        hukou[2] && ($scope.data.personOne.resid_area = hukou[2])
        //现住地址 省市区 
        let newAddr = ($scope.cityid2||'').split(',')
        newAddr[0] && ($scope.data.personOne.addr_province = newAddr[0])
        newAddr[1] && ($scope.data.personOne.addr_city = newAddr[1])
        newAddr[2] && ($scope.data.personOne.addr_area = newAddr[2])
        //公司地址 省市区 
        let compAddr = ($scope.cityid3||'').split(',')
        compAddr[0] && ($scope.data.company.addr_province = compAddr[0])
        compAddr[1] && ($scope.data.company.addr_city = compAddr[1])
        compAddr[2] && ($scope.data.company.addr_area = compAddr[2])
        //房产地址 省市区 
        let houseAddr = ($scope.cityid4||'').split(',')
        houseAddr[0] && ($scope.data.housing.addr_province = houseAddr[0])
        houseAddr[1] && ($scope.data.housing.addr_city = houseAddr[1])
        houseAddr[2] && ($scope.data.housing.addr_area = houseAddr[2])
        //联系人家庭地址 省市区 
        let linkAddr = ($scope.cityid5||'').split(',')
        linkAddr[0] && ($scope.data.linkMan.addr_province = linkAddr[0])
        linkAddr[1] && ($scope.data.linkMan.addr_city = linkAddr[1])
        linkAddr[2] && ($scope.data.linkMan.addr_area = linkAddr[2])

        $scope.data.person[0] = $scope.data.personOne
        formData.person = JSON.stringify($scope.data.person)    //基本信息转json字符串
        formData.linkMan = JSON.stringify($scope.data.linkMan)   //联系人信息转json字符串
        formData.json1 = JSON.stringify($scope.data.json1)   //其他联系人1信息转json字符串
        formData.json2 = JSON.stringify($scope.data.json2)    //其他联系人2信息转json字符串
        formData.json3 = JSON.stringify($scope.data.json3)    //其他联系人3信息转json字符串
        formData.housing = JSON.stringify($scope.data.housing)    //房产信息转json字符串
        formData.car = JSON.stringify($scope.data.car)    //车辆信息转json字符串
        formData.company = JSON.stringify($scope.data.company)    //公司信息转json字符串
        $scope.data.order.loan_type = $scope.loanState;
        $scope.data.order.s_id = sId
        formData.order =  JSON.stringify($scope.data.order)   //订单信息转json字符串(加密)
        formData.fee = JSON.stringify($scope.data.fee)    //费用信息转json字符串
        formData.propertyPersonal = JSON.stringify($scope.data.propertyPersonal)    //产权人信息转json字符串
        formData.orderAttachmentList = JSON.stringify($scope.attaArr)    //资料清单转json字符串

        //如果是编辑订单就会有oid、pid
        $scope.searchData.oid && (formData.oid = $scope.searchData.oid)
        $scope.searchData.pid && (formData.pid = $scope.searchData.pid)

        http.post('insertOrder.do', formData, function (data) {

            if(data.errcode == 1){
                $scope.successToast(data.errmsg)
                $scope.searchData.oid = data.data.oid
                $scope.searchData.pid = data.data.pid
                $scope.searchData.opear = sId
                $scope.searchData.msg = JSON.stringify($scope.msgFill)
                $state.go('application.subsaveok', $scope.searchData)
            }else{
                $scope.errorToast(data.errmsg || '提交失败！')
            }
        })
    }
    /**
     * tab菜单切换
     */
    $scope.show = function (params, ele) {
        //当params为借款人 借款人信息
        if (params == 'loanMess' || params == 'dataList') {
            $_fn.applyShow(params,$scope.apply)
            //切换菜单
            // $('.public-PoBefore-nav li a').removeClass('active').removeClass('activePublic')
            // $(ele.target).addClass('active')
        }
    }
    $scope.select = [{name:'以租代购',value:1},{name:'信贷方式',value:2}]
    /*********************资料清单*******************/
    /**
     * 重新重置为数组
     */
    // $_fn.ueditorImg(function(data){
    //     //默认加载时直接实例化 页面上属性data-tag:查找节点 name:唯一
    //     // $rootScope.uploads.attachImg = data)							//把返回来的url数组传给后台
    // })
    //获取到类别才能去实例
    $scope.$watch('option.dataList',function (newVal) {
        if(newVal){
            $timeout(function () {
                //重新重置为数组
                $_fn.ueditorImg(function (data, _btn) {
                    // console.log('watch-A', data, newVal, _btn,$scope.data.orderAttachmentList.attach_id)
                    //当前选择的上传类型
                    let attachId  = $scope.uploads.attach_id
                    if(!$scope.attaArr) {
                        $scope.attaArr = []
                    }
                    const activeAttaarr = $scope.option.dataList.find((ii) => ii.id == attachId)
                    let index = $scope.attaArr.indexOf(activeAttaarr)    //获取所在数组的索引

                    $scope.$apply(function(){
                        if(index > -1){
                            //图片大于1个的时候追加,没有的时候新增
                            if($scope.attaArr[index].attach_img.length >= 1){
                                $scope.attaArr[index].attach_img = $scope.attaArr[index].attach_img.concat(data)
                            } else {
                                $scope.attaArr[index].attach_img = data
                            }
                            // $rootScope.uploads.attachImg = JSON.stringify($scope.attaArr[index].attach_img)
                        }
                        if(index == -1){
                            activeAttaarr.attach_img = []
                            activeAttaarr.attach_img = activeAttaarr.attach_img.concat(data)
                            $scope.attaArr.push(activeAttaarr)
                        }

                        // for(let i=0; i<$scope.attaArr.length; i++){
                            // console.log('watch', i)
                            // $_fn.imgList(0)
                            // setTimeout(function(){
                            //   var viewer = new Viewer(document.getElementById('gallery0'),{
                            //       url: 'data-original',
                            //       show: function (){
                            //           viewer.update();
                            //       }
                            //   });
                            // },300)
                        // }

                    })

                })
                // $('#imgTerm').find('div').removeClass('webuploader-pick')
            })
        }
    })
    //图片挂载查看图片函数
    /* $scope.$watch('attaArr',function (newVal) {
        console.log('watch-attaArr', newVal)
        if(newVal){
            // for(){}
        }
    },true) */
    //监听资料填写情况
    $scope.$watch('data',function (newVal) {
        console.log('watch-data！！：', newVal)
        for(let i in $scope.msgFill){
            $scope.msgFill[i] = false
        }
        //借款
        for(let i in newVal.order){
            if(newVal.order[i]){
                $scope.msgFill.loanOk = true
                break
            }
        }
        //基本
        for(let i in newVal.person){
            for(let k in newVal.person[i]){
                if(newVal.person[i][k]){
                    $scope.msgFill.baseOk = true
                    break
                }
            }
        }
        for(let i in newVal.personOne){
            if(newVal.personOne[i]){
                $scope.msgFill.baseOk = true
                break
            }
        }
        //公司
        for(let i in newVal.company){
            if(newVal.company[i]){
                $scope.msgFill.companyOk = true
                break
            }
        }
        //carOk
        for(let i in newVal.car){
            if(newVal.car[i]){
                $scope.msgFill.carOk = true
                break
            }
        }
        //houseOk
        for(let i in newVal.housing){
            if(newVal.housing[i]){
                $scope.msgFill.houseOk = true
                break
            }
        }
        //linkManOk
        for(let i in newVal.linkMan){
            if(newVal.linkMan[i]){
                $scope.msgFill.linkManOk = true
                break
            }
        }
        //dataListOk
        for(let i in newVal.attaArr){
            if(newVal.attaArr[i]){
                $scope.msgFill.dataListOk = true
                break
            }
        }
        if($scope.msgFill.loanOk ||$scope.msgFill.baseOk ||$scope.msgFill.companyOk ||
            $scope.msgFill.carOk ||$scope.msgFill.houseOk||$scope.msgFill.linkManOk)
        {
                $scope.msgFill.applyOk = true
        }


        /* if(newVal.order.lt_id && newVal.order.apply_money && newVal.order.at_id && 
            newVal.order.repayment_way && newVal.order.apply_money 
        ){
            $scope.msgFill.loanOk = true
        } */
    },true)
    //图片预加载
    $scope.loadImage = $_fn.loadImage;
    //图片查看
    $scope.imgList = $_fn.imgList;
    //删除方法
    $scope.deleAtta = function(id){
        $scope.attaArr = $scope.attaArr.filter(function(item){
            return item.id != id
        })
    }
    $scope.deleImg = function(e,key,id){
        $_fn.stopBOM(e)													//阻止默认事件
        var a = parseInt(e.target.getAttribute('data-idx'))
        var attach_img = $scope.attaArr[key].attach_img
        if (attach_img.length==1) {
            $scope.deleAtta(id);
        } else {
            attach_img.splice(a,1)  //删除数组
        }
        // console.log('$scope.attaArr2:', $scope.attaArr, id, a)
    }
    /**
     * 可移动菜单 
     */ 
    var loanMenu = document.querySelector('.loanmenu-fixed_1')
    function animation(ele){
        if(ele){
            ele.onmousedown = function(e){
                e = e || window.event
                var X = e.offsetX
                var Y = e.offsetY
                console.log('XY', X, Y)
                document.onmousemove = function(ev){
                    var hh = $(document).height()-180
                    var ww = $(document).width()-170
                    ev = ev || window.event
                    console.log('ww-hh',ww,hh)
                    console.log(ev.clientX,ev.clientY)
                    var newleft = ev.clientX - X 
                    var newtop = ev.clientY - Y
                    var fixed = document.querySelector('.loanmenu-fixed')
                    if(newleft > 0 && newleft <= ww){
                        fixed.style.left = ev.clientX - X + 'px'
                    }
                    if(newtop > 0 && newtop <= hh){
                        fixed.style.top = ev.clientY - Y + 'px'
                    }
                    ev.preventDefault()
                    loanMenu.onclick = null
                }
                e.preventDefault()
                document.onmouseup = function(e){
                    // console.log(e)
                    document.onmousemove = null
                    // setTimeout(function(){
                    //     loanMenu.onclick = showAllMes
                    // },200)
                }
            }
        }
    }
    animation(loanMenu)
}]